﻿using System.Collections.ObjectModel;
using System.IO;
using System.Net;
using Windows.UI.Xaml.Controls;
using dot_in.HTWKAidStation.Client;
using dot_in.HTWKAidStation.Client.Menu;

namespace dot_in.HTWKAidStation.GUI.Views
{
    public sealed partial class MenuView : Page
    {
        public MenuView()
        {
            this.InitializeComponent();

            this.WeeklyMenuItems = new ObservableCollection<WeeklyMenuItem>();
            const string baseString = "2012-10-";
            for (int i = 23; i < 29; i++)
            {
                this.LoadData(baseString + i);
            }
        }
        
        public ObservableCollection<WeeklyMenuItem> WeeklyMenuItems { get; set; }

        private void LoadData(string dateString)
        {
            HttpWebRequest request =
                WebRequest.CreateHttp("http://www.studentenwerk-leipzig.de/mensa/menu?location=118&date=" + dateString);
            WebResponse response = request.GetResponseAsync().Result;
            StreamReader reader = new StreamReader(response.GetResponseStream());
            MenuParser parser = new MenuParser(reader.ReadToEnd());

            WeeklyMenuItem item = new WeeklyMenuItem(dateString);
            foreach (MenuItem tmp in parser.GetAllMenuItems())
            {
                item.MenuItems.Add(tmp);
            }

            this.WeeklyMenuItems.Add(item);
        }

    }

    public class WeeklyMenuItem
    {
        public WeeklyMenuItem(string dateString)
        {
            this.DateString = dateString;
            this.MenuItems = new ObservableCollection<MenuItem>();
        }

        public string DateString { get; set; }

        public ObservableCollection<MenuItem> MenuItems { get; set; }
    }
}